package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.PageFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnLogicNode extends Shape {

	public static final String LOGIC_PARAMETER_KEYS_SELECTION_LOGIC_XML = ".logic";

	public static final String TRANSACTIONTYPE_PROPERTY = "transactiontype_property";

	public static final String SHOW_LOGIC_NODE_EXCEPTION = "exception";

	public static final String KEY_NODE_LOGIC = "logic";

	public static final String SHOW_NODE_LOGIC = Messages
			.getString("SaturnLogicNode.logic"); //$NON-NLS-1$

	public static final String KEY_LOGIC_NODE_NAME = "_property_name";

	public static final String KEY_LOGIC_NODE_KEY = "_property_key";

	public static final String KEY_LOGIC_NODE_DESCRITPION = "_property_description";

	public static final String KEY_LOGIC_NODE_PARAMETERS = "_property_parameters";

	public static final String SHOW_LOGIC_NODE_NAME = "name";

	public static final String SHOW_LOGIC_NODE_KEY = "key";

	public static final String SHOW_LOGIC_NODE_DESCRITPION = "description";

	public static final String SHOW_LOGIC_NODE_PARAMETERS = "parameters";

	public static final String SHOW_LOGIC_NODE_TRANSACTION = "transaction";

	public static final String SHOW_LOGIC_NODE_TRANSACTIONTYPE = "type";

	private static final long serialVersionUID = 1L;

	public static final String KEY_LOGIC_NODE_EXCEPTION = "EXCEPTION";

	private String key = "";

	private String exception = "";

	private String transactionType = SelectionCandidate.transactionTypesForLogic[0];

	private Image image = Activator.getImage(Images.LOGIC_ICON);
	private static int number = 1;

	@Override
	public Image getIcon() {
		return image;
	}
	public SaturnLogicNode(){
		super.setName(SHOW_NODE_LOGIC + number++);
	}

	@Override
	public IFigure getFigure() {
		PageFigure figure = new PageFigure();
		if (super.isIschanged() == false) {
			Label imageLabel = new Label(image);
			imageLabel.setLocation(new Point(figure.getLocation().x + 90,
					figure.getLocation().y));
			figure.getImageFigure().add(imageLabel);
			Font nameFont = new Font(null, "Arial", 74, SWT.BOLD);
			Label namelabel = new Label(super.getName());
			namelabel.setFont(nameFont);
			// namelabel.setSize(72, 74);
			figure.getLetterFigure().add(namelabel);
		}

		return figure;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		if (!this.key.equals(key)) {
			this.key = key;
			firePropertyChange(KEY_LOGIC_NODE_KEY, null, key);
		}
	}

	public String getException() {
		return exception;
	}

	public void setException(String exception) {
		this.exception = exception;
		firePropertyChange(KEY_LOGIC_NODE_KEY, null, exception);
	}

	public Node doRefresh() {
		Node root = super.getRoot();

		Node logicNode = SaturnW3CUtil.createChildNode(root, KEY_NODE_LOGIC,
				null);

		SaturnW3CUtil.createChildNode(logicNode, SHOW_LOGIC_NODE_KEY, this.key);

		SaturnW3CUtil.createAttribute(logicNode, SHOW_LOGIC_NODE_EXCEPTION,
				this.exception);

		SaturnW3CUtil.createChildNode(logicNode, SHOW_LOGIC_NODE_NAME, super
				.getName());

		this.createTransactionType(logicNode);
		super.createParemeters(logicNode);

		return logicNode;
	}

	/**
	 * 创建Logic的transactionType
	 * 
	 */
	public void createTransactionType(Node node) {

		Node transactionNode = SaturnW3CUtil.createChildNode(node,
				SHOW_LOGIC_NODE_TRANSACTION, null);

		SaturnW3CUtil.createAttribute(transactionNode,
				SHOW_LOGIC_NODE_TRANSACTIONTYPE, this.transactionType);
	}

	public Shape cloneShape(Point mousePoint, Shape shape) {
		return null;
	}

	protected void setImage(Image image) {
		this.image = image;
	}
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"logic"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "logic",
					observer, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	

	public String getTransactionType() {
		return transactionType;
	}

	public void setTransactionType(String transactionType) {
		if (!this.transactionType.equals(transactionType)) {
			this.transactionType = transactionType;
			firePropertyChange(TRANSACTIONTYPE_PROPERTY, null, transactionType);
		}
	}
	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		Map<String, List<?>> summm = new HashMap<String, List<?>>();
		List<SaturnParameter> parameters = getParameters();
		String name = getName();
		Parameters para = new Parameters();
		String url = "";
		String[] keys = new String[10];

		para.setName(getName());
		para.setId(getId());
		para.setExperssion(getKey());
		
		//创建逻辑构件表达式的链接
		if (!"".equals(getKey()) && getKey().endsWith(".logic")) {
	 		url = getKey().replace(".logic", "");
	 		keys = url.split("/");
	 		if (keys[0].equals(tempnames[0] + "::")) {
	 			if (keys[1].equals(tempnames[1])) {
	 				if (keys[2].equals(tempnames[2])) {
	 					url = "../" + keys[3] + "-logic/" + keys[3] + "-logic.html";
	 				} else {
	 					url = "../../" + keys[2] +"/" + keys[3] 
	 					                    + "-logic/" + keys[3] + "-logic.html";
	 				}
	 			} else {
	 				url = "../../../" + keys[1] +"/" + keys[2] +"/" 
	 										+ keys[3] + "-logic/" + keys[3] + "-logic.html";
	 			}
	 		} else {
	 			if (!url.split("/")[0].equals("edu.ccut.saturn.basecomponent::"))	 {
	 				url = "../../../../" + keys[0].replace("::", "") + "/" 
						+ keys[1] +"/" + keys[2] +"/" + keys[3] + "-logic/" + keys[3] + "-logic.html";
	 			}
	 		}
	 	}
		para.setUrl(url);
		para.setDescription(getDescription());
		para.setTransactiontype(getTransactionType());
	 	basicvaluse.add(para);
	 	
		para.setInfourl( name + "-logic.html");
		para.setInfoname( name );
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.LogicNode")); //$NON-NLS-1$
		para.setImagename("logic");
		summm.put("parameters", parameters);
		summm.put("basicvaluse", basicvaluse);
		String templatePath = "template/export/logic/logic";
		String Path = filePath + File.separator + name + "-logic.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,summm, Path);
		
		return para;
	}
}